package com.dhcc.wb.user;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.dhcc.common.database.DBManager;
import com.dhcc.common.util.StringUtil;
import com.dhcc.wb.gongzhonghao.GzhModel;

public class UserDao {
	private static final Logger logger = Logger.getLogger(UserDao.class);
	
	
	
	
	
	/*
	 * 查询
	 */
	@SuppressWarnings("unchecked")
	public List<UserModel> findQiyeServiceByPage(Integer pageNumber,Integer pageSize,String gzhid) {
		DBManager dbm = null;
		List<UserModel> list = null;
		try {
			dbm = new DBManager();
			String sql = "select * from weixin_user where 1=1 ";
				   if(!StringUtil.isNullOrEmpty(gzhid)){
					   sql += " and gzhid = '"+ gzhid +"'";			   
				   }
				   sql += " order by ref_date desc";
			       sql +=  " limit " + pageNumber + ","+ pageSize +"";
			list = dbm.getObjectList(UserModel.class, sql);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			dbm.close();
		}
		return list;
	}
	
		
	//新增
	public int addUser1(String ids, String ref_date,Integer user_source,Integer new_user,Integer cancel_user,String gzhid,String gzhname) {
		int i=0;
		DBManager dbm = null;
		try{
			dbm = new DBManager();
			String sql = "insert into weixin_user(ids,ref_date,user_source,new_user,cancel_user,gzhid,gzhname) values('"+ ids +"','"+ ref_date +"','"+ user_source +"','"+new_user+"','"+cancel_user+"','"+gzhid+"','"+gzhname+"')";
			i = dbm.executeUpdate(sql);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			dbm.close();
		}
		return i;
	}
	//更新
	public int updateUser1(String ids, Integer user_source,Integer new_user,Integer cancel_user,String gzhname) {
		int i = 0;
		DBManager dbm = null;
		try{
			dbm = new DBManager();
			String sql = "update weixin_user set user_source = '"+ user_source +"',new_user='"+new_user+"',cancel_user='"+cancel_user+"',gzhname='"+gzhname+"' where ids = '"+ ids +"'";
			i = dbm.executeUpdate(sql);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			dbm.close();
		}
		return i;
	}
	
	
	//新增
	public int addUser2(String ids, String ref_date,Integer cumulate_user,String gzhid,String gzhname) {
		int i=0;
		DBManager dbm = null;
		try{
			dbm = new DBManager();
			String sql = "insert into weixin_user(ids,ref_date,cumulate_user,gzhid,gzhname) values('"+ ids +"','"+ ref_date +"','"+ cumulate_user +"','"+gzhid+"','"+gzhname+"')";
			i = dbm.executeUpdate(sql);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			dbm.close();
		}
		return i;
	}
	//更新
	public int updateUser2(String ids,Integer cumulate_user,String gzhname) {
		int i = 0;
		DBManager dbm = null;
		try{
			dbm = new DBManager();
			String sql = "update weixin_user set cumulate_user = '"+ cumulate_user +"',gzhname='"+gzhname+"' where ids = '"+ ids +"'";
			i = dbm.executeUpdate(sql);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			dbm.close();
		}
		return i;
	}
	
	
	//编辑
	public UserModel queryqiye_result(String gzhid,String ref_date) {
		UserModel model = null;
		DBManager dbm = null;
		try{
			dbm = new DBManager();
			String sql = "select * from weixin_user "
					+ " where gzhid = '"+ gzhid +"' and ref_date='"+ref_date+"'";
			model = (UserModel) dbm.getObject(UserModel.class, sql);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			dbm.close();
		}
		return model;
	}
	
	
	//公众号的关注人数
	public Integer findMax(String gzhid) {
		Integer maxs = 0;
		DBManager dbm = null;
		UserModel model = null;
		try{
			dbm = new DBManager();
			String sql = "select * from weixin_user  where gzhid = '"+ gzhid +"' order by ref_date desc";
			List<UserModel> list = dbm.getObjectList(UserModel.class, sql);
			if(list != null && list.size()>0){
				model = list.get(0);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			dbm.close();
		}
		return (model != null)?(model.getCumulate_user()!=null?model.getCumulate_user():0):0;
	}
	
	//每个公众号的关注用户排行榜
	@SuppressWarnings("unchecked")
	public List<UtilUserModel>  interUserByGzh(List<GzhModel> gzhlist) {
		DBManager dbm = null;
		List<UtilUserModel> list = new ArrayList<UtilUserModel>();
		try{
			dbm = new DBManager();
//			if(gzhlist != null && gzhlist.size()>0){
//				StringBuffer sb = new StringBuffer("SELECT t.gzhid,t.gzhname,MAX(t.cumulate_user) AS cumulate_user FROM weixin_user t GROUP BY t.gzhid ");
//				sb.append( " having gzhid in (");
//				for(GzhModel gzh : gzhlist){
//					sb.append("'"+gzh.getGzhid()+"',");
//				}
//				sb.deleteCharAt(sb.length()-1);
//				sb.append(")");
//				sb.append(" ORDER BY cumulate_user DESC");
//				list = dbm.getObjectList(UserModel.class, sb.toString());
//			}
			
			if(gzhlist != null && gzhlist.size()>0){
				
				for(GzhModel gzh : gzhlist){
					String sql = "select * from weixin_user  where gzhid = '"+ gzh.getGzhid() +"' order by ref_date desc";
					UtilUserModel model = (UtilUserModel) dbm.getObject(UtilUserModel.class, sql);
					if(model != null){list.add(model);}
				}
			}
			
			
			
			
			
			
			
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			dbm.close();
		}
		return list;
	}
	
	//今日新增
	public Integer findNew(String gzhid,String ref_date) {
		Integer count = 0;
		DBManager dbm = null;
		UserModel model = null;
		try{
			dbm = new DBManager();
			String sql = "select * from weixin_user  where gzhid = '"+ gzhid +"' and ref_date = '"+ref_date+"'";
			model = (UserModel) dbm.getObject(UserModel.class, sql);
			if(model != null && model.getNew_user() != null){
				count =  model.getNew_user();
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			dbm.close();
		}
		return count;
	}	
	//今日取消
	public Integer findCancal(String gzhid,String ref_date) {
		Integer count = 0;
		DBManager dbm = null;
		UserModel model = null;
		try{
			dbm = new DBManager();
			String sql = "select * from weixin_user  where gzhid = '"+ gzhid +"' and ref_date = '"+ref_date+"'";
			model = (UserModel) dbm.getObject(UserModel.class, sql);
			if(model != null && model.getCancel_user() != null){
				count =  model.getCancel_user();
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			dbm.close();
		}
		return count;
	}
}
